import {request} from '../../utils/request.js';
import {mockQuest} from '../../utils/mockQuest.js';
Page({
  data:{
    leftMenuList:[],
    rightMenuList:[],
    currentLeftmenuIndex:0,
    currentRightMenu:[],
    rightScrollTop:0
  },
  onLoad:function(options) {
    let productCategoryLeftmenuList = wx.getStorageSync('productCategoryLeftmenuList');
    let productCategoryList = wx.getStorageSync('productCategoryList');
    if (!productCategoryLeftmenuList || !productCategoryList) {
      console.log('商品分类未缓存，现请求新数据');
      this.getCates();
    } else {
      if (Date.now() - productCategoryLeftmenuList['time'] > 1000 * 10 || Date.now() - productCategoryList['time'] > 1000 * 10) {
        console.log('商品分类缓存已过期，现请求新数据');
        this.getCates();
      } else {
        console.log('商品分类缓存未过期，可以使用');
        this.setData({
          leftMenuList:productCategoryLeftmenuList['data'],
          rightMenuList:productCategoryList['data'],
          currentRightMenu:productCategoryList['data'][0]
        });
      }
    }
  },
  onShow:function(options) {
  },
  getCates() {
    mockQuest({url:'WeChat_Index_categoryList'})
      .then(result => {
        let resCode = result['code'], resData = result['data'];
        if (resCode === 200 && Object.prototype.toString.call(resData) === '[object Object]' && JSON.stringify(resData) !== '{}') {
          wx.setStorageSync('productCategoryLeftmenuList', {time:Date.now(), data:resData['productCategoryLeftmenuList']});
          wx.setStorageSync('productCategoryList', {time:Date.now(), data:resData['productCategoryList']});
          this.setData({
            leftMenuList:resData['productCategoryLeftmenuList'],
            rightMenuList:resData['productCategoryList'],
            currentRightMenu:resData['productCategoryList'][0]
          });
        }
      });
  },
  onTapLeftmenu(ev) {
    const {index} = ev.currentTarget.dataset;
    this.setData({
      currentLeftmenuIndex:index,
      currentRightMenu:this.data.rightMenuList[index],
      rightScrollTop:0
    });
  }
});